package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.MyReflectionAdapter;
import com.runen.infinitemovement.model.api.ChallengeAPI;
import com.runen.infinitemovement.model.entity.AchievementEntity;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Challenge;

public class MyReflectionFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_my_reflection)
    RecyclerView rvMyReflection;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private View view;
    private MyReflectionAdapter myReflectionAdapter;
    private String targetId;

    public static Fragment newInstance(String key) {
        Fragment fragment = new MyReflectionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("targetId", key);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_my_reflect, container, false);
            ButterKnife.bind(this, view);
            targetId = getArguments().getString("targetId");
            initView();
        }
        return view;
    }

    private void initView() {
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvMyReflection.setLayoutManager(layoutManager);
        myReflectionAdapter = new MyReflectionAdapter(getActivity());
        rvMyReflection.setAdapter(myReflectionAdapter);
        initData();
    }

    private void initData() {
        srl.setRefreshing(true);
        String str = "?targetId=%s";
        new ChallengeAPI().getAchievement(String.format(str, targetId), new BaseResultCallback<AchievementEntity>() {
            @Override
            public void onResponse(AchievementEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        myReflectionAdapter.setNewData(response.data);
                    }
                }else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @Override
    public void onRefresh() {
       initData();
    }
}
